package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.ProjectsProgress;
import com.ruoyi.system.mapper.ProjectsProgressMapper;
import com.ruoyi.system.response.ProjectsInfo;
import com.ruoyi.system.service.IProjectsProgressService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 项目进度描述Service业务层处理
 *
 * @author tanshen
 * @date 2023-10-12
 */
@Service
public class ProjectsProgressServiceImpl extends ServiceImpl<ProjectsProgressMapper, ProjectsProgress> implements IProjectsProgressService {
    @Autowired
    private ProjectsProgressMapper projectsProgressMapper;

    /**
     * 查询项目进度描述
     *
     * @param id 项目进度描述主键
     * @return 项目进度描述
     */
    @Override
    public ProjectsProgress selectProjectsProgressById(Long id) {
        return projectsProgressMapper.selectProjectsProgressById(id);
    }

    /**
     * 查询项目进度描述列表
     *
     * @param projectsProgress 项目进度描述
     * @return 项目进度描述
     */
    @Override
    public List<ProjectsProgress> selectProjectsProgressList(ProjectsProgress projectsProgress) {
        return projectsProgressMapper.selectProjectsProgressList(projectsProgress);
    }

    /**
     * 新增项目进度描述
     *
     * @param projectsProgress 项目进度描述
     * @return 结果
     */
    @Override
    public int insertProjectsProgress(ProjectsProgress projectsProgress) {
        projectsProgress.setCreateTime(DateUtils.getNowDate());
        projectsProgress.setUpdateTime(DateUtils.getNowDate());
        projectsProgress.setCreateBy(ShiroUtils.getLoginName());
        return projectsProgressMapper.insertProjectsProgress(projectsProgress);
    }

    /**
     * 修改项目进度描述
     *
     * @param projectsProgress 项目进度描述
     * @return 结果
     */
    @Override
    public int updateProjectsProgress(ProjectsProgress projectsProgress) {
        projectsProgress.setUpdateTime(DateUtils.getNowDate());
        projectsProgress.setUpdateBy(ShiroUtils.getLoginName());
        return projectsProgressMapper.updateProjectsProgress(projectsProgress);
    }

    /**
     * 批量删除项目进度描述
     *
     * @param ids 需要删除的项目进度描述主键
     * @return 结果
     */
    @Override
    public int deleteProjectsProgressByIds(String ids) {
        return projectsProgressMapper.deleteProjectsProgressByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除项目进度描述信息
     *
     * @param id 项目进度描述主键
     * @return 结果
     */
    @Override
    public int deleteProjectsProgressById(Long id) {
        return projectsProgressMapper.deleteProjectsProgressById(id);
    }

    @Override
    public ProjectsInfo selectProjectsProgressByProjectId(Long projectId, ProjectsInfo projects) {
        QueryWrapper<ProjectsProgress> wrapper = new QueryWrapper<>();
        wrapper.eq("project_id", projectId);
        wrapper.orderByDesc("create_time");
        List<ProjectsProgress> list = projectsProgressMapper.selectList(wrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            Map<Integer, List<ProjectsProgress>> map = list.stream().collect(Collectors.groupingBy(ProjectsProgress::getType));
            List<ProjectsProgress> progressList = map.get(0);
            if (CollectionUtils.isNotEmpty(progressList)) {
                List<ProjectsProgress> collect = progressList.stream().sorted(Comparator.comparing(BaseEntity::getCreateTime).reversed()).collect(Collectors.toList());
                projects.setProgressList(collect);
            }
            List<ProjectsProgress> progressList2 = map.get(1);
            if (CollectionUtils.isNotEmpty(progressList2)) {
                List<ProjectsProgress> collect = progressList2.stream().sorted(Comparator.comparing(BaseEntity::getCreateTime).reversed()).collect(Collectors.toList());
                projects.setTechnicalAgreementProgressList(collect);
            }
        }
        return projects;
    }

    @Override
    public List<ProjectsProgress> selectProgress(List<Long> projectIds) {
        return projectsProgressMapper.selectProgress(projectIds);
    }
}
